package com.mbc.common.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Properties;

import com.mbc.common.entities.LinkServer;
import com.mbc.common.exception.DataNotFoundException;

public class DBManagerFactory {

	private static HashMap<String, Properties> connectionMap = new HashMap<String, Properties>();
	private static HashMap<String, String> dataSourceMap = new HashMap<String, String>();
		
	public static void addDataSource(String sessionId, String dataSource){
		dataSourceMap.put(sessionId, dataSource);
	}
	
	public static String getDataSource(String sessionId){
		return dataSourceMap.get(sessionId);
	}
	
	public static void deleteDataSource(String sessionId){
		dataSourceMap.remove(sessionId);
	}
	
	public static void addProperties(String sessionId, Properties properties)
	{
		try {
			connectionMap.put(sessionId, properties);
		} catch (Exception e) {
		}
	}
	
	public static void deleteProperties(String sessionId)
	{
		try {
			connectionMap.remove(sessionId);
		} catch (Exception e) {
		}
	}
	
	public static DBManager getDBManager() throws Exception
	{
		return new DBManager();
	}
	
	public static DBManager getDBManager(int linkType) throws Exception
	{
		DBManager l_dbManger = new DBManager();
		
		LinkServerDAO l_LinkServerDAO = CommonDAOFactory.getLinkServerDAO(l_dbManger.getConnection());
		Connection l_linkedConn;
		try {
			LinkServer l_LinkServer = l_LinkServerDAO.findByLinkType(linkType);
			if(l_LinkServer == null)
				throw new DataNotFoundException("No Linked Server with Type : " + linkType + ".");
			String l_Url = "jdbc:jtds:sqlserver://" + l_LinkServer.getServer() + (l_LinkServer.getPort() == 0 ? "" : ":" + l_LinkServer.getPort()) + "/" + l_LinkServer.getDB_Name() + ";" + (l_LinkServer.getInstance().equals("") ? "" : "Instance=" + l_LinkServer.getInstance());
			l_linkedConn = DriverManager.getConnection(l_Url, l_LinkServer.getLogin_Id(), l_LinkServer.getPassword());
		} catch (Exception e) {
			throw e;
		} finally {
			l_dbManger.closeConnection();
		}
		
		return new DBManager(l_linkedConn);
	}
	
	public static DBManager getDBManager(String sessionId)
	{
		Properties l_props = (Properties)connectionMap.get(sessionId);
		Connection l_Conn = null;
		try {
			Class.forName(l_props.getProperty("driver"));
			l_Conn = DriverManager.getConnection(l_props.getProperty("url"), l_props.getProperty("username"), l_props.getProperty("password"));
		} catch (Exception e) {
			
		}
		
		return new DBManager(l_Conn);
	}
	
	public static DBManager getDBManager(String sessionId, int linkType)throws Exception{
		Properties l_props = (Properties)connectionMap.get(sessionId);
		Connection l_Conn = null;
		try {
			Class.forName(l_props.getProperty("driver"));
			l_Conn = DriverManager.getConnection(l_props.getProperty("url"), l_props.getProperty("username"), l_props.getProperty("password"));
		} catch (Exception e) {
			
		}
		
		LinkServerDAO l_LinkServerDAO = CommonDAOFactory.getLinkServerDAO(l_Conn);
		Connection l_linkedConn;
		try {
			LinkServer l_LinkServer = l_LinkServerDAO.findByLinkType(linkType);
			if(l_LinkServer == null)
				throw new DataNotFoundException("No Linked Server with Type : " + linkType + ".");
			String l_Url = "jdbc:jtds:sqlserver://" + l_LinkServer.getServer() + (l_LinkServer.getPort() == 0 ? "" : ":" + l_LinkServer.getPort()) + "/" + l_LinkServer.getDB_Name() + ";" + (l_LinkServer.getInstance().equals("") ? "" : "Instance=" + l_LinkServer.getInstance());
			l_linkedConn = DriverManager.getConnection(l_Url, l_LinkServer.getLogin_Id(), l_LinkServer.getPassword());
		} catch (Exception e) {
			throw e;
		} finally {
			l_Conn.close();
		}
		
		return new DBManager(l_linkedConn);
	}
}
